using System;
#if NETFX_CORE
using Windows.UI.Xaml.Data;
#else
using System.Globalization;
using System.Windows.Data;
using System.Windows.Input;
#endif

namespace FreshMeat.Xaml
{
#if !SILVERLIGHT && !NETFX_CORE
   [System.Windows.Markup.MarkupExtensionReturnType(typeof(IValueConverter))]
#endif
#if NETFX_CORE
   public class BoolToCursorExtension : IValueConverter
   {
#else
   public class BoolToCursorExtension : System.Windows.Markup.MarkupExtension, IValueConverter
   {
      static BoolToCursorExtension converter;

      public override object ProvideValue(IServiceProvider serviceProvider)
      {
         return converter ?? (converter = new BoolToCursorExtension());
      }
#endif

#if NETFX_CORE
      public object Convert(object value, Type targetType, object parameter, string culture)
#else
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
#endif
      {
#if !NETFX_CORE
         var wantsCursor = (bool)value;
         if (wantsCursor)
         {
            var cursorName = parameter as string;
            if (cursorName != null)
            {
               var property = typeof(Cursors).GetProperty(cursorName);
               if (property == null)
                  throw new ArgumentException(string.Format("{0} is not a valid cursor name.", cursorName));

               var cursor = (Cursor)property.GetValue(null, null);
               return cursor;
            }
         }
#endif
         return null; // Null means "no specific cursor was specified"
      }

 #if NETFX_CORE
      public object ConvertBack(object value, Type targetType, object parameter, string culture)
#else
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
#endif
      {
#if !NETFX_CORE
         var cursor = value as Cursor;
         return cursor != null;
#else
         return true;
#endif
      }
   }
}